/* eslint-disable space-before-function-paren */
/* eslint-disable eqeqeq */

function isSameGroup(id: string, chessBoardID: string) {
  return (!chessBoardID.match(/[A-Z]/)) === (!id.match(/[A-Z]/))
}

export default class ChessRelated {
  chessBoard: any
  constructor(chessBoard: any) {
    this.chessBoard = chessBoard
  }

  /**
   * @description 判断是否可以操作棋子
   * @param chooseID 选中的id
   * @param chessCharacter 1-表示先手 2-表示后手
   */
  canChessOperation(chooseID: string, chessCharacter: number) {
    if (!chooseID) return false
    if (!chessCharacter) return false
    if (chessCharacter == 1) {
      if (!chooseID.match(/[a-z]/)) {
        console.log('你不能操作该棋子')
        return false
      }
    } else if (chessCharacter == 2) {
      if (!chooseID.match(/[A-Z]/)) {
        console.log('你不能操作该棋子')
        return false
      }
    } else {
      console.error('chessCharacter是个无效值')
      return false
    }
    return true
  }

  getAllMovePosition(isRedCamp: any, ChessList: any, chooseID: string, chessCharacter: number) {
    const chessBoard = this.chessBoard
    if (chessCharacter == 2) isRedCamp = !isRedCamp
    const getMovesFnMap: any = {
      b: function (id: string, x: number, y: number) {
        const res = []
        let findChessID = ''
        if (isRedCamp && x > 0) {
          // 红棋向上检索
          findChessID = chessBoard[x - 1][y]
          if (!findChessID || !isSameGroup(id, findChessID)) {
            res.push([x - 1, y])
          }
        } else if (x < 9) {
          // 黑棋向下检索
          findChessID = chessBoard[x + 1][y]
          if (!findChessID || !isSameGroup(id, findChessID)) {
            res.push([x + 1, y])
          }
        }
        // 兵过河左右查找
        if ((x < 5 && isRedCamp) || (x > 4 && !isRedCamp)) {
          if (y > 0) findChessID = chessBoard[x][y - 1]
          if (!findChessID || !isSameGroup(id, findChessID)) {
            res.push([x, y - 1])
          }
          if (y < 8) findChessID = chessBoard[x][y + 1]
          if (!findChessID || !isSameGroup(id, findChessID)) {
            res.push([x, y + 1])
          }
        }
        return res
      },

      s: function (id: string, x: number, y: number) {
        const res = []
        let findChessID = ''
        // 4点半
        if ((y + 1 <= 5 && x < 9 && isRedCamp) || (x + 1 <= 2 && y + 1 <= 5 && !isRedCamp)) {
          findChessID = chessBoard[x + 1][y + 1]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x + 1, y + 1])
        }
        // 7点半
        if ((y - 1 >= 3 && x < 9 && isRedCamp) || (x + 1 <= 2 && y - 1 >= 3 && !isRedCamp)) {
          findChessID = chessBoard[x + 1][y - 1]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x + 1, y - 1])
        }
        // 1点半
        if ((x - 1 >= 7 && y + 1 <= 5 && isRedCamp) || (y + 1 <= 5 && x > 0 && !isRedCamp)) {
          findChessID = chessBoard[x - 1][y + 1]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x - 1, y + 1])
        }
        // 10点半
        if ((x - 1 >= 7 && y - 1 >= 3 && isRedCamp) || (y - 1 >= 3 && x > 0 && !isRedCamp)) {
          findChessID = chessBoard[x - 1][y - 1]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x - 1, y - 1])
        }
        return res
      },

      x: function (id: string, x: number, y: number) {
        const res = []
        let findChessID = ''
        // 4点半
        if (x + 2 < 10 && y + 2 < 9 && !chessBoard[x + 1][y + 1]) {
          if (isRedCamp || (x + 2 <= 4 && !isRedCamp)) {
            findChessID = chessBoard[x + 2][y + 2]
            if (!findChessID || !isSameGroup(id, findChessID)) res.push([x + 2, y + 2])
          }
        }
        // 7点半
        if (x + 2 < 10 && y - 2 >= 0 && !chessBoard[x + 1][y - 1]) {
          if (isRedCamp || (x + 2 <= 4 && !isRedCamp)) {
            findChessID = chessBoard[x + 2][y - 2]
            if (!findChessID || !isSameGroup(id, findChessID)) res.push([x + 2, y - 2])
          }
        }
        // 1点半
        if (x - 2 >= 0 && y + 2 < 9 && !chessBoard[x - 1][y + 1]) {
          if ((x - 2 >= 5 && isRedCamp) || !isRedCamp) {
            findChessID = chessBoard[x - 2][y + 2]
            if (!findChessID || !isSameGroup(id, findChessID)) res.push([x - 2, y + 2])
          }
        }
        // 10点半
        if (x - 2 >= 0 && y - 2 >= 0 && !chessBoard[x - 1][y - 1]) {
          if ((x - 2 >= 5 && isRedCamp) || !isRedCamp) {
            findChessID = chessBoard[x - 1][y - 1]
            if (!findChessID || !isSameGroup(id, findChessID)) res.push([x - 2, y - 2])
          }
        }
        return res
      },

      j: function (id: string, x: number, y: number) {
        const res = []
        let findChessID = ''
        const isNull = (() => {
          const x1 = ChessList.j1.x
          const x2 = ChessList.J1.x
          for (let i = x1 - 1; i > x2; i--) {
            if (chessBoard[i][y]) return false
          }
          return true
        })()
        // 下
        if ((x + 1 < 10 && isRedCamp) || (x + 1 <= 2 && !isRedCamp)) {
          findChessID = chessBoard[x + 1][y]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x + 1, y])
        }

        // 上
        if ((x - 1 >= 7 && isRedCamp) || (x - 1 >= 0 && !isRedCamp)) {
          findChessID = chessBoard[x - 1][y]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x - 1, y])
        }
        // 老将对老将的情况
        if (ChessList.J1.y == ChessList.j1.y && isNull) {
          res.push([ChessList.J1.x, y])
        }
        // 右
        if (y + 1 <= 5) {
          findChessID = chessBoard[x][y + 1]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x, y + 1])
        }
        // 左
        if (y - 1 >= 3) {
          findChessID = chessBoard[x][y - 1]
          if (!findChessID || !isSameGroup(id, findChessID)) res.push([x, y - 1])
        }

        return res
      },

      p: function (id: string, x: number, y: number) {
        const res = []
        // 上检索
        let n = 0
        for (let i = x - 1; i >= 0; i--) {
          if (chessBoard[i][y]) {
            if (n == 0) {
              n++
              continue
            } else {
              if (!isSameGroup(id, chessBoard[i][y])) res.push([i, y])
              break
            }
          } else {
            if (n == 0) res.push([i, y])
          }
        }
        // 下检索
        n = 0
        for (let i = x + 1; i <= 9; i++) {
          if (chessBoard[i][y]) {
            if (n == 0) {
              n++
              continue
            } else {
              if (!isSameGroup(id, chessBoard[i][y])) res.push([i, y])
              break
            }
          } else {
            if (n == 0) res.push([i, y])
          }
        }
        // 左检索
        n = 0
        for (let i = y - 1; i >= 0; i--) {
          if (chessBoard[x][i]) {
            if (n == 0) {
              n++
              continue
            } else {
              if (!isSameGroup(id, chessBoard[x][i])) res.push([x, i])
              break
            }
          } else {
            if (n == 0) res.push([x, i])
          }
        }
        // 右检索
        n = 0
        for (let i = y + 1; i <= 8; i++) {
          if (chessBoard[x][i]) {
            if (n == 0) {
              n++
              continue
            } else {
              if (!isSameGroup(id, chessBoard[x][i])) res.push([x, i])
              break
            }
          } else {
            if (n == 0) res.push([x, i])
          }
        }
        return res
      },

      m: function (id: string, x: number, y: number) {
        const res = []
        // 1点
        if (x - 2 >= 0 && y + 1 <= 8 && !chessBoard[x - 1][y] && (!chessBoard[x - 2][y + 1] || !isSameGroup(id, chessBoard[x - 2][y + 1]))) res.push([x - 2, y + 1])
        // 2点
        if (x - 1 >= 0 && y + 2 <= 8 && !chessBoard[x][y + 1] && (!chessBoard[x - 1][y + 2] || !isSameGroup(id, chessBoard[x - 1][y + 2]))) res.push([x - 1, y + 2])
        // 4点
        if (x + 1 <= 9 && y + 2 <= 8 && !chessBoard[x][y + 1] && (!chessBoard[x + 1][y + 2] || !isSameGroup(id, chessBoard[x + 1][y + 2]))) res.push([x + 1, y + 2])
        // 5点
        if (x + 2 <= 9 && y + 1 <= 8 && !chessBoard[x + 1][y] && (!chessBoard[x + 2][y + 1] || !isSameGroup(id, chessBoard[x + 2][y + 1]))) res.push([x + 2, y + 1])
        // 7点
        if (x + 2 <= 9 && y - 1 >= 0 && !chessBoard[x + 1][y] && (!chessBoard[x + 2][y - 1] || !isSameGroup(id, chessBoard[x + 2][y - 1]))) res.push([x + 2, y - 1])
        // 8点
        if (x + 1 <= 9 && y - 2 >= 0 && !chessBoard[x][y - 1] && (!chessBoard[x + 1][y - 2] || !isSameGroup(id, chessBoard[x + 1][y - 2]))) res.push([x + 1, y - 2])
        // 10点
        if (x - 1 >= 0 && y - 2 >= 0 && !chessBoard[x][y - 1] && (!chessBoard[x - 1][y - 2] || !isSameGroup(id, chessBoard[x - 1][y - 2]))) res.push([x - 1, y - 2])
        // 11点
        if (x - 2 >= 0 && y - 1 >= 0 && !chessBoard[x - 1][y] && (!chessBoard[x - 2][y - 1] || !isSameGroup(id, chessBoard[x - 2][y - 1]))) res.push([x - 2, y - 1])

        return res
      },

      c: function (id: string, x: number, y: number) {
        const res = []
        // 上检索
        for (let i = x - 1; i >= 0; i--) {
          if (!chessBoard[i][y]) {
            res.push([i, y])
            continue
          } else if (!isSameGroup(id, chessBoard[i][y])) {
            res.push([i, y])
          }
          break
        }
        // 下检索
        for (let i = x + 1; i <= 9; i++) {
          if (!chessBoard[i][y]) {
            res.push([i, y])
            continue
          } else if (!isSameGroup(id, chessBoard[i][y])) {
            res.push([i, y])
          }
          break
        }
        // 左检索
        for (let i = y - 1; i >= 0; i--) {
          if (!chessBoard[x][i]) {
            res.push([x, i])
            continue
          } else if (!isSameGroup(id, chessBoard[x][i])) {
            res.push([x, i])
          }
          break
        }
        // 右检索
        for (let i = y + 1; i <= 8; i++) {
          if (!chessBoard[x][i]) {
            res.push([x, i])
            continue
          } else if (!isSameGroup(id, chessBoard[x][i])) {
            res.push([x, i])
          }
          break
        }
        return res
      }

    }
    return getMovesFnMap[chooseID.slice(0, 1).toLocaleLowerCase()](chooseID, ChessList[chooseID].x, ChessList[chooseID].y)
  }

  checkCanMove(arr: any[], x: any, y: any) {
    let res = false
    if (!arr || !Array.isArray(arr)) {
      console.log(`arr不合法：${arr}`, arr)
      return res
    }
    arr.some(v => {
      if (v[0] == x && v[1] == y) {
        res = true
        return res
      }
    })
    return res
  }
}
